/**
 * Created by gouyuheng on 2017/3/2.
 * 该js基于jquery和vue的插件，在引入之前必须先引入jquery和vue
 */

(function (Vue) {

    Vue.prototype.isPermission = function (func, permissions) {
        for (let i=0; i<permissions.length; i++) {
            if (func === permissions[i]) {
                return true
            }
        }
        return false
    }

    $(function () {
        let pathname = window.location.pathname;
        if ($('a[href="'+pathname+'"]').length !== 0) {
            $($('a[href="'+pathname+'"]')[0].parentNode).addClass('active');
        }
    })

    // 页面发送Post请求
    Vue.prototype.http = function (url, data, success, error) {
        $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify(data),
            success: success,
            error: error
        })
    };

    // 页面发送get请求
    Vue.prototype.$get = function (url, data, success, error) {
        $.ajax({
            url: url,
            type: 'get',
            dataType: 'json',
            contentType: 'application/json',
            success: success,
            error: error
        })
    };

    // 页面中保存目录展开关闭状态
    Vue.prototype.saveSession = function (sessionName) {
        if (window.sessionStorage.getItem(sessionName) === "block") {
            window.sessionStorage.setItem(sessionName, "none");
        } else {
            window.sessionStorage.setItem(sessionName, "block");
        }
    }
    
    // 页面中时间格式转换
    Vue.prototype.formatTime = function (time, needHour) {
        if (time !== undefined && time !== '' && time instanceof Date) {
            let newTime = '';
            // if (needHour === undefined) {
            //     needHour = false;
            // }
            if (needHour) {
                newTime = time.getFullYear() + "-" + (time.getMonth() + 1)
                    + "-" + time.getDate() + " " + time.getHours() + ":"
                    + time.getMinutes() + ":" + time.getSeconds();
            } else {
                newTime = time.getFullYear() + "-" + (time.getMonth() + 1)
                    + "-" + time.getDate();
            }
            return newTime;
        } else {
            return time;
        }
    }

    // 页面中获取当前时间
    Vue.prototype.getNow = function () {
        let date = new Date();
        let newTime = date.getFullYear() + "-" + (date.getMonth() + 1)
            + "-" + date.getDate() + " " + date.getHours() + ":"
            + date.getMinutes() + ":" + date.getSeconds();
        return newTime;
    }

    // 页面中获取字典列表
    Vue.prototype.getDictionary = function (code, obj) {
        $.ajax({
            url: "/dictionary/list/" + code,
            type: 'get',
            dataType: 'json',
            contentType: 'application/json',
            success: function (res) {
                if (res.status === 100) {
                    obj = res.returnValue;
                } else {
                    let error = {
                        value: "error",
                        name: "error"
                    }
                    obj.push(error);
                }
            },
            error: function (res) {
                let error = {
                    value: "error",
                    name: "error"
                }
                obj.push(error);
            }
        })
    }

})(Vue)